﻿using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Application.Purchase;
using System;
using System.Collections.Generic;
using System.Data;
using Aspose.Cells;
using System.IO;

namespace FriendStar.Web.Areas.Purchase.Controllers
{
    public class MaterialPurchaseController : ControllerBase
    {
        private string useUnit = Configs.GetValue("UseUnit");

        private string factoryFlag = Configs.GetValue("FactoryFlag");

        private string curUser = OperatorProvider.Provider.GetCurrent().UserName;

        private string MYDESC = "台账日期材料采购";

        private string UseUnit = Configs.GetValue("UseUnit");

        MaterialPurchaseApp app = new MaterialPurchaseApp();

        public ActionResult ContractRegistration()
        {
            ViewData["LedgerDate"] = app.GetLedgerDate(useUnit, factoryFlag, MYDESC, curUser);

            ViewData["curUserName"] = OperatorProvider.Provider.GetCurrent().UserName;

            ViewData["curUnit"] = useUnit;

            ViewData["curDate"] = DateTime.Now.ToString("yyyy-MM-dd");

            ViewData["curTime"] = DateTime.Now.ToString("HH:mm:ss");

            return View();
        }

        public ActionResult ContractRegistrationAdd()
        {
            return View();
        }
        public ActionResult ContractRegistrationImport()
        {
            return View();
        }
        public ActionResult ContractRegistrationMaterialSelect()
        {
            return View();
        }
        public ActionResult PlanAndOrderContract()
        {
            ViewData["LedgerDate"] = app.GetLedgerDate(useUnit, factoryFlag, MYDESC, curUser);

            ViewData["curUserName"] = OperatorProvider.Provider.GetCurrent().UserName;

            ViewData["curUnit"] = useUnit;

            ViewData["curDate"] = DateTime.Now.ToString("yyyy-MM-dd");

            ViewData["curTime"] = DateTime.Now.ToString("HH:mm:ss");

            return View();
        }

        public ActionResult AddContractRegistration(string postData)
        {
            DateTime now = DateTime.Now;

            Dictionary<string, object> item = new Dictionary<string, object>();

            item = postData.ToObject<Dictionary<string, object>>();

            object data = null;

            item.Add("UseUnit", useUnit);

            if (item["ID"].Equals(string.Empty))
            {
                string guid = Guid.NewGuid().ToString();

                item["ID"] = guid;

                item.Add("CGDJRY", curUser);

                item.Add("CGDJRQ", now.ToString("yyyy-MM-dd"));

                item.Add("CGDJSJ", now.ToString("HH:mm:ss"));

                data = app.AddContractRegistration(item);
            }
            else
            {
                item.Add("CGFBRY", curUser);

                item.Add("CGFBRQ", now.ToString("yyyy-MM-dd"));

                item.Add("CGFBSJ", now.ToString("HH:mm:ss"));

                data = app.UpdateContractRegistration(item);
            }

            return Content(data.ToJson());
        }

        public ActionResult DeleteContractRegistration(string ids)
        {
            Dictionary<string, object> item = new Dictionary<string, object>();

            var data = app.DeleteContractRegistration(ids);

            return Content(data.ToJson());
        }

        //public ActionResult PublishContractRegistration(string ids)
        //{
        //    DateTime now = DateTime.Now;

        //    string curDate = now.ToString("yyyy-MM-dd");

        //    string curTime = now.ToString("HH:mm:ss");

        //    var data = app.PublishContractRegistration(ids, curUser,curDate,curTime);

        //    return Content(data.ToJson());
        //}

        public ActionResult ImportContractRegistration(string postData)
        {
            DateTime now = DateTime.Now;

            Dictionary<string, object> item = new Dictionary<string, object>();

            item = postData.ToObject<Dictionary<string, object>>();

            item.Add("UseUnit", useUnit);

            string base64Str = item["FILECONTENT"].ToString();

            string date = item["HTRQ"].ToString();

            string curDate = now.ToString("yyyy-MM-dd");

            string curTime = now.ToString("HH:mm:ss");

            byte[] bt = Convert.FromBase64String(base64Str);

            Dictionary<string, object> ds = ImportExcel(bt);

            var data = app.ImportContractRegistration(date, ds, useUnit, curUser, curDate, curTime);

            return Content(data.ToJson());
        }

        public Dictionary<string, object> ImportExcel(byte[] fileContent)
        {
            Workbook workbook = null;

            Dictionary<string, object> ds = null;

            List<Dictionary<string, object>> dt = null;

            Dictionary<string, object> dr = null;

            List<string> cols = null;

            workbook = new Workbook(new MemoryStream(fileContent));

            ds = new Dictionary<string, object>();

            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                Cells cells = workbook.Worksheets[i].Cells;

                cols = new List<string>();

                dt = new List<Dictionary<string, object>>();

                for (int j = 0; j < cells.MaxDataRow + 1; j++)
                {
                    if (0 == cols.Count)
                    {
                        for (int k = 0; k < cells.MaxDataColumn + 1; k++)
                        {
                            cols.Add(cells[j, k].Value.ToString());
                        }
                    }
                    else
                    {
                        dr = new Dictionary<string, object>();

                        for (int k = 0; k < cells.MaxDataColumn + 1; k++)
                        {
                            dr.Add(cols[k], cells[j, k].Value);
                        }

                        dt.Add(dr);
                    }
                }

                ds.Add(workbook.Worksheets[i].Name, dt);
            }

            return ds;
        }

        public ActionResult GetFProductionTypies(string FClassNO)
        {
            var data = app.GetFProductionTypies(FClassNO, useUnit);

            return Content(data.ToJson());
        }


        public ActionResult GetSupplierInformationies(int idx)
        {
            var data = app.GetSupplierInformationies(useUnit, idx);

            return Content(data.ToJson());
        }
        public ActionResult GetMaterialCategories()
        {
            var data = app.GetMaterialCategories(useUnit);

            return Content(data.ToJson());
        }

        public ActionResult GetContractRegistrationsAdd(Pagination pagination, string CGCD, string CGDZ, string CGCGFL, string CGCGFS, string CGYT, string CGGHDW, string CGCGFLiu)
        {
            var data = new
            {
                rows = app.GetContractRegistrationsAdd(pagination, CGCD, CGDZ, CGCGFL, CGCGFS, CGYT, CGGHDW, CGCGFLiu, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };

            return Content(data.ToJson());
        }
        public ActionResult GetContractRegistrationsArchive(Pagination pagination, string CGCD, string CGDZ, string CGCGFL, string CGCGFS, string CGYT, string CGGHDW, string CGCGFLiu)
        {
            var data = new
            {
                rows = app.GetContractRegistrationsArchive(pagination, CGCD, CGDZ, CGCGFL, CGCGFS, CGYT, CGGHDW, CGCGFLiu, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };

            return Content(data.ToJson());
        }
        //public ActionResult GetMaterialCategoriesByPage(Pagination pagination, string MaterialNO, string MaterialCode,string MaterialSizes)
        //{
        //    var data = new
        //    {
        //        rows = app.GetMaterialCategoriesByPage(pagination, MaterialNO, MaterialCode, MaterialSizes, useUnit),
        //        total = pagination.total,
        //        page = pagination.page,
        //        records = pagination.records
        //    };

        //    return Content(data.ToJson());
        //}

    }
}
